jQuery(document).ready(floatFooter);
jQuery(window).scroll(floatFooter);
jQuery(window).resize(floatFooter);


function floatFooter() {

    var winHeight = jQuery(window).height();
    var winTop = jQuery(window).scrollTop();
    var floaterHeight = jQuery('#floating-footer').height();
    var floaterTop = jQuery('#floating-footer').offset().top;
    var footerTop = jQuery('#floating-footer-container').offset().top;

    if (jQuery('#floating-footer').hasClass('locked') && floaterTop > footerTop) {
        jQuery('#floating-footer').removeClass('locked');
    } else if (!jQuery('#floating-footer').hasClass('locked') && (floaterTop + floaterHeight) > (winHeight + winTop)) {
        jQuery('#floating-footer').addClass('locked');
    }

    if (jQuery('#floating-footer').hasClass('garden')) {

        var winWidth = jQuery(window).width();
        var wrapWidth = jQuery('.wrapper').width();

        var treeLeft = 901;
        var birdLeft = -300;

        if (winWidth < wrapWidth) {
            treeLeft -= (wrapWidth - winWidth) / 2;
            birdLeft -= (wrapWidth - winWidth) / 2;
        }

        if (jQuery('#floating-footer .tree').is(':visible')) {

            jQuery('#floating-footer .tree').css('left', treeLeft);

            var treeWinLeft = jQuery('#floating-footer .tree').offset().left;
            var treeWidth = jQuery('#floating-footer .tree').width();
            var treeNewWidth = (winWidth - treeWinLeft > 281 ? 281 : winWidth - treeWinLeft);

            jQuery('#floating-footer .tree').css('width', treeNewWidth);

        }

        if (jQuery('#floating-footer .bird').is(':visible')) {

            jQuery('#floating-footer .bird').css('left', birdLeft);

            var birdWinLeft = jQuery('#floating-footer .bird').offset().left;
            var birdNewLeft = (birdWinLeft < 0 ? birdLeft - birdWinLeft : birdLeft);
            var birdNewWidth = (birdWinLeft < 0 ? 243 + birdWinLeft : 243);
            birdNewWidth = (birdNewWidth < 0 ? 0 : birdNewWidth);

            jQuery('#floating-footer .bird').css('left', birdNewLeft).css('width', birdNewWidth);

        }
    }
}